#include "component_session.h"
#include "../objects/object_session_data.h"
#include "../common/utils.h"

struct _ComponentSession
{
    GtkBox parent_instance;
    ObjectSessionData *session_data;
};

enum
{
    PROP_0,
    PROP_SESSION_DATA,
    N_PROPERTIES
};

G_DEFINE_TYPE(ComponentSession, component_session, GTK_TYPE_BOX)
static GParamSpec *obj_properties[N_PROPERTIES] = {
    NULL,
};

static void component_session_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
    ComponentSession *self = COMPONENT_SESSION(object);

    switch (property_id)
    {
    case PROP_SESSION_DATA:
        if (self->session_data)
        {
            g_object_unref(self->session_data);
        }
        self->session_data = g_value_dup_object(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        return;
    }
}

static void component_session_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
    ComponentSession *self = COMPONENT_SESSION(object);

    switch (property_id)
    {
    case PROP_SESSION_DATA:
        g_value_set_object(value, self->session_data);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        break;
    }
}

static void component_session_class_init(ComponentSessionClass *klass)
{
    gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(klass), "/src/components/component_session.ui");
    utils_load_css_from_resource("/src/components/component_session.css");

    GObjectClass *object_class = G_OBJECT_CLASS(klass);
    object_class->set_property = component_session_set_property;
    object_class->get_property = component_session_get_property;

    obj_properties[PROP_SESSION_DATA] = g_param_spec_object("session_data", "Session Data", "Session Data", OBJECT_TYPE_SESSION_DATA, G_PARAM_READWRITE);

    g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);
}

static void component_session_init(ComponentSession *self)
{
    gtk_widget_init_template(GTK_WIDGET(self));
}
